package com.ruoyi.user.service.impl;

import java.math.BigInteger;
import java.security.SecureRandom;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.user.domain.*;
import com.ruoyi.user.mapper.UserInfoMapper;
import com.ruoyi.user.mapper.UserInformationInfoMapper;
import com.ruoyi.user.service.UserInfoService;
import com.ruoyi.user.utils.AliPicUtils;
import com.ruoyi.user.utils.CodeUtils;
import com.ruoyi.user.utils.SfzUtis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImpl implements UserInfoService {
//    static String co;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserInformationInfoMapper userInformationInfoMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    //登录
    public ResultVo getshow(UserInfo userInfo) {
        //查询用户所有信息
        UserInfo getshow = userInfoMapper.getshow(userInfo);

        if (getshow == null){

            return new ResultVo(500,"账户或密码错误",null);
            //存入redis中

        }
        if (getshow.getIsStatus()==2){
            return new ResultVo(500,"该账号已停用",null);
        }

        UserVo userVo = userInfoMapper.seleall(getshow.getCode());
        String token=UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(token, String.valueOf(userVo));
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",userVo);
        return new ResultVo(200,"查询成功",map);
    }

    @Override
    //登录验证码
    public ResultVo loginCode(String phone) {
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        Integer selectphone = selectphone(userInfo);
        if (selectphone != 0){
            //手机号是否为空
            if (phone != null){
                //手机号是否为11位
                if (phone.length() == 11){
                    //手机号开头是否为1
                    if (phone.startsWith("1")){
                        //创建codeutile
                        CodeUtils codeUtils = new CodeUtils();
                        //传递手机号发送验证码
                        Integer code = codeUtils.phoneCode(phone);
                        redisTemplate.boundValueOps(phone).set(String.valueOf(code),60, TimeUnit.SECONDS); //设置过期时间为60秒，60秒后自动删除
                        return new ResultVo(200,"发送成功",null);
                    }else {
                        return new ResultVo(500,"手机号错误",null);
                    }
                }
                return new ResultVo(500,"手机号错误",null);
            }
            return new ResultVo(500,"手机号错误",null);
        }
        return new ResultVo(500,"没有该手机号",null);
    }

    //修改用户信息
    @Override
    public ResultVo updateuser(UserInformationInfo userInfo) {
        userInformationInfoMapper.updateuser(userInfo);
        return new ResultVo(200,"修改成功",null);
    }

    @Override
    public ResultVo delete(String code) {
        redisTemplate.delete(code);
        return new ResultVo(200,"删除成功",null);
    }


    @Override
    public UserVo getCode(String code) {
        return userInfoMapper.seleall(code);
    }

static String plusImgs; //正面身份证信=地址
static  ReVo reVo= new ReVo();//正面身份证信息
    @Override
    public ReVo update(MultipartFile plusImg) throws Exception {
        //判断身份证是否传过来
        if (plusImg!=null){
            String imgser = AliPicUtils.upload(plusImg);
            ReVo reVo1 = SfzUtis.postForm(imgser);
            System.out.println(reVo1.getData()+"+++++");
            plusImgs=imgser;
            reVo1.setImgser(imgser);
            reVo.setData(reVo1.getData());
            System.out.println(reVo+"-------");
            return reVo1;
        }
        return new ReVo(500,"请上传身份证");
    }

static String minusImgs; //反面身份证地址
    @Override
    public ReVo updatemin(MultipartFile minusImg) throws Exception {
        if (minusImg!=null){
            String imgser = AliPicUtils.upload(minusImg);
            ReVo reVo1 = SfzUtis.postForm(imgser);
            minusImgs=imgser;
            reVo1.setImgser(imgser);
            return reVo1;
        }
        return new ReVo(500,"请上传身份证");
    }
//用来刷新个人信息用户
    @Override
    public ResultVo getusercode(String code) {
        UserVo userVo = userInfoMapper.seleall(code);
        return new ResultVo(200,"查询成功",userVo);
    }

    //上传身份证后修改个人用户信息
    @Override
    public ResultVo imgsfz(String code) throws ParseException {
        UserInformationInfo userInformationInfo = new UserInformationInfo();
        userInformationInfo.setPlusImg(plusImgs);
        userInformationInfo.setMinusImg(minusImgs);
        if(reVo!=null){
            Data data1 = (Data) reVo.getData();
            System.out.println(data1);
            SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
            String date = yyyyMMdd.format(new Date());
            int age = Integer.parseInt(date.substring(0, 4)) - Integer.parseInt(data1.getBirthday().substring(0, 4));
            userInformationInfo.setAge(age);
            if (data1.getSex().equals("男")){
                userInformationInfo.setSex(1);
            }else {
                userInformationInfo.setSex(2);
            }
            Date parse = new SimpleDateFormat("yyyyMMdd").parse(data1.getBirthday());
            userInformationInfo.setBirthday(parse);
            userInformationInfo.setNumbers(data1.getNumber());
            System.out.println("++++++++++++++++++++++++++++++++++++++++++++++"+userInformationInfo+"+++");
            userInformationInfo.setCode(code);
            userInformationInfo.setName(data1.getName());
            userInformationInfo.setStatus(1);
            userInformationInfo.setAddress(data1.getAddress());
            userInformationInfoMapper.updateimg(userInformationInfo);
            return new ResultVo(200,"修改成功",null);
        }else {
            return new ResultVo(500,"请上传身份证",null);
        }

    }


    //注册验证码
    @Override
    public ResultVo sendCode(String phone) {
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        Integer i = selectphone(userInfo);
        if (i == 0){
            //注册
            //手机号是否为空
            if (phone != null){
                //手机号是否为11位
                if (phone.length() == 11){
                    //手机号开头是否为1
                    if (phone.startsWith("1")){
                        //创建codeutile
                        CodeUtils codeUtils = new CodeUtils();
                        //传递手机号发送验证码
                        Integer code = codeUtils.phoneCode(phone);
                        redisTemplate.boundValueOps(phone).set(String.valueOf(code),60, TimeUnit.SECONDS); //设置过期时间为60秒，60秒后自动删除
                        return new ResultVo(200,"发送成功",null);
                    }else {
                        return new ResultVo(500,"手机号错误",null);
                    }
                }
                return new ResultVo(500,"手机号错误",null);
            }
            return new ResultVo(500,"手机号错误",null);
        }
        return new ResultVo(500,"手机号重复请换个手机号",null);
    }

    //手机号登录
    @Override
    public ResultVo phone(UserInfo userInfo) {
        String code = stringRedisTemplate.opsForValue().get(userInfo.getPhone());
        if (code.equals(userInfo.getCde())){
            UserVo userInfo1= userInfoMapper.getAll(userInfo.getPhone());
            System.out.println(userInfo1);
            if (userInfo1 != null){
                return new ResultVo(200,"登录成功",userInfo1);
            }else {
                return new ResultVo(500,"用户不存在",null);
            }
        }
        return new ResultVo(500,"验证码错误",null);
    }

    @Override
    //修改密码
    public ResultVo password(UserInfo userInfo) {
        UserInfo all = userInfoMapper.getAll(userInfo.getPhone());
        if (all == null){
            return new ResultVo(500,"没有该手机号",null);
        }
        userInfoMapper.updatepassword(userInfo);
        return new ResultVo(200,"修改成功",null);
    }



    @Override
    //注册
    public ResultVo add(UserInfo userInfo) {
        //redis获取验证码
        String code = stringRedisTemplate.opsForValue().get(userInfo.getPhone());
        System.out.println(code+"*******");
        //判断redis验证码是否为空和前端传过来的是否为空
        if (code!=null&& userInfo.getCde()!=null){
            System.out.println(userInfo.getCde()+"--------");
            if (code.equals(userInfo.getCde())) {
                //判断账户是否重复
                Integer integer = select(userInfo);
                System.out.println(integer+"++++++");
                if (integer == 0){
                    //判断手机号是否重复
                    Integer phone = selectphone(userInfo);
                    if (phone==0){
                        //添加创建时间
                        userInfo.setCreateTime(new Date());
                        //输出创建时间
                        System.out.println(new Date()+"创建时间");
                        //随机数co
                        String string = UUID.randomUUID().toString();
                        //存入code中
                        userInfo.setCode(string);
                        userInfo.setIsStatus(1);
                        System.out.println(code+"+++++++++");
                        //添加方法
                        userInfoMapper.add(userInfo);
                        //需要给其他的页面也创建占用资源
                        UserInformationInfo userInformationInfo = new UserInformationInfo();
                        userInformationInfo.setPhone(userInfo.getPhone());
                        userInformationInfo.setCode(string);
                        userInformationInfo.setBankCode(string);
                        userInformationInfo.setRoleCode("1");
                        userInformationInfo.setStatus(0);
                        userInformationInfo.setPic("https://binzimg.oss-cn-beijing.aliyuncs.com/h2306a/Snipaste_2023-09-18_11-46-28.png");
                        userInfoMapper.addUser(userInformationInfo);
                        //+1
                        BankInfo bankInfo = new BankInfo();
                        bankInfo.setBankCode(string);
                        userInfoMapper.addBankInfo(bankInfo);
                        return new ResultVo(200, "添加成功", null);
                    }
                    return new ResultVo(500,"手机号重复",null);
                }
                return new ResultVo(500,"账户重复",null);
            }else {
                return new ResultVo(500, "验证码错误", null);
            }
        }
       return new ResultVo(500,"验证码错误",null);
    }



    //判断账户是否重复
    public Integer select(UserInfo userInfo){
        //查询账户是否重复返回值
        Integer integer = userInfoMapper.selectone(userInfo);
        return integer;
    }
    //判断手机号是否重复
    public Integer selectphone(UserInfo userInfo){
        //判断手机号是否重复
        Integer integer = userInfoMapper.selectphone(userInfo);
        return integer;
    }
}
